#include <regx52.h>

sbit DS1302_SCK = P1^1;
sbit DS1302_IO = P1^2;
sbit DS1302_CE = P1^3;

#define DS1302_SECOND			0x80
#define DS1302_MINUTE			0x82
#define DS1302_HOUR				0x84
#define DS1302_DATE				0x86
#define DS1302_MONTH			0x88
#define DS1302_DAY				0x8A
#define DS1302_YEAR				0x8C
#define DS1302_WP				0x8E
#define DS1302_CHA				0x90

#define DS1302_WRITE_RAM_0		0xC0
#define DS1302_WRITE_RAM_1		0xC2
#define DS1302_WRITE_RAM_2		0xC4
#define DS1302_WRITE_RAM_3		0xC6
#define DS1302_WRITE_RAM_4		0xC8
#define DS1302_WRITE_RAM_5		0xCA
#define DS1302_WRITE_RAM_6		0xCC
#define DS1302_WRITE_RAM_7		0xCE
#define FLAG_PFProtection		0XAA		//随便定义的一个数

char DS1302_Time[] = {24,12,12,8,51,55,2};

void DS1302_Init(void)
{
	DS1302_CE = 0;
	DS1302_SCK = 0;
}

unsigned char DS1302_ReadByte(unsigned char Command)
{
	unsigned char i;
	unsigned char Data = 0x00;
	Command |= 0x01;
	DS1302_CE = 1;
	
	for(i=0;i<8;i++)
	{
		DS1302_IO = Command & (0x01<<i);
		DS1302_SCK = 0;
		DS1302_SCK = 1;
	}
	
	for(i=0;i<8;i++)
	{
		DS1302_SCK = 1;
		DS1302_SCK = 0;
		if(DS1302_IO == 1){Data |= (0x01<<i);}
	}
	
	DS1302_CE = 0;
	DS1302_IO = 0;
	return Data;
}

void DS1302_WriteByte(unsigned char Command,Data)
{
	unsigned char i;
	DS1302_CE = 1;
	
	for(i=0;i<8;i++)
	{
		DS1302_IO = Command & (0x01<<i);
		DS1302_SCK = 1;//这里要考虑时钟芯片能接受的速度
		DS1302_SCK = 0;//如果单片机运行速度太快，这两句代码间要加一定的延时（C51很慢不用加延时）
	}
	
	for(i=0;i<8;i++)
	{
		DS1302_IO = Data & (0x01<<i);
		DS1302_SCK = 1;//这里要考虑时钟芯片能接受的速度
		DS1302_SCK = 0;//如果单片机运行速度太快，这两句代码间要加一定的延时（C51很慢不用加延时）
	}
	
	DS1302_CE = 0;	
}

void DS1302_SetTime(void)
{
	DS1302_WriteByte(DS1302_WP,0x00);
	DS1302_WriteByte(DS1302_YEAR,DS1302_Time[0]/10*16+DS1302_Time[0]%10);
	DS1302_WriteByte(DS1302_MONTH,DS1302_Time[1]/10*16+DS1302_Time[1]%10);
	DS1302_WriteByte(DS1302_DATE,DS1302_Time[2]/10*16+DS1302_Time[2]%10);
	DS1302_WriteByte(DS1302_HOUR,DS1302_Time[3]/10*16+DS1302_Time[3]%10);
	DS1302_WriteByte(DS1302_MINUTE,DS1302_Time[4]/10*16+DS1302_Time[4]%10);
	DS1302_WriteByte(DS1302_SECOND,DS1302_Time[5]/10*16+DS1302_Time[5]%10);
	DS1302_WriteByte(DS1302_DAY,DS1302_Time[6]/10*16+DS1302_Time[6]%10);
	DS1302_WriteByte(DS1302_WP,0x80);
	
}

void DS1302_ReadTime(void)
{
	unsigned char Temp;
	Temp = DS1302_ReadByte(DS1302_YEAR);
	DS1302_Time[0] = Temp/16*10 + Temp%16;
	Temp = DS1302_ReadByte(DS1302_MONTH);
	DS1302_Time[1] = Temp/16*10 + Temp%16;
	Temp = DS1302_ReadByte(DS1302_DATE);
	DS1302_Time[2] = Temp/16*10 + Temp%16;
	Temp = DS1302_ReadByte(DS1302_HOUR);
	DS1302_Time[3] = Temp/16*10 + Temp%16;
	Temp = DS1302_ReadByte(DS1302_MINUTE);
	DS1302_Time[4] = Temp/16*10 + Temp%16;
	Temp = DS1302_ReadByte(DS1302_SECOND);
	DS1302_Time[5] = Temp/16*10 + Temp%16;
	Temp = DS1302_ReadByte(DS1302_DAY);
	DS1302_Time[6] = Temp/16*10 + Temp%16;
}

void DS1302_Charger(void)
{
	DS1302_WriteByte(DS1302_CHA,0xa5);
}


//void DS1302_PFProtection_Init(void)			//掉电保护代码编写尝试（未成工）
//{
//	
//	if(DS1302_ReadByte(DS1302_WRITE_RAM_7) != FLAG_PFProtection)
//	{
//		DS1302_WriteByte(DS1302_WRITE_RAM_0,DS1302_Time[0]/10*16+DS1302_Time[0]%10);
//		DS1302_WriteByte(DS1302_WRITE_RAM_1,DS1302_Time[1]/10*16+DS1302_Time[1]%10);
//		DS1302_WriteByte(DS1302_WRITE_RAM_2,DS1302_Time[2]/10*16+DS1302_Time[2]%10);
//		DS1302_WriteByte(DS1302_WRITE_RAM_3,DS1302_Time[3]/10*16+DS1302_Time[3]%10);
//		DS1302_WriteByte(DS1302_WRITE_RAM_4,DS1302_Time[4]/10*16+DS1302_Time[4]%10);
//		DS1302_WriteByte(DS1302_WRITE_RAM_5,DS1302_Time[5]/10*16+DS1302_Time[5]%10);
//		DS1302_WriteByte(DS1302_WRITE_RAM_6,DS1302_Time[6]/10*16+DS1302_Time[6]%10);
//		
//		DS1302_WriteByte(DS1302_WRITE_RAM_7,FLAG_PFProtection);
//	}
//	else
//	{
//		unsigned char Temp;
//		Temp = DS1302_ReadByte(DS1302_WRITE_RAM_0);
//		DS1302_Time[0] = Temp/16*10 + Temp%16;
//		Temp = DS1302_ReadByte(DS1302_WRITE_RAM_1);
//		DS1302_Time[1] = Temp/16*10 + Temp%16;
//		Temp = DS1302_ReadByte(DS1302_WRITE_RAM_2);
//		DS1302_Time[2] = Temp/16*10 + Temp%16;
//		Temp = DS1302_ReadByte(DS1302_WRITE_RAM_3);
//		DS1302_Time[3] = Temp/16*10 + Temp%16;
//		Temp = DS1302_ReadByte(DS1302_WRITE_RAM_4);
//		DS1302_Time[4] = Temp/16*10 + Temp%16;
//		Temp = DS1302_ReadByte(DS1302_WRITE_RAM_5);
//		DS1302_Time[5] = Temp/16*10 + Temp%16;
//		Temp = DS1302_ReadByte(DS1302_WRITE_RAM_6);
//		DS1302_Time[6] = Temp/16*10 + Temp%16;
//	}
//}




